/*
* $Id: InfoDrawer.cpp 122 2010-07-22 17:05:50Z volok-aleksej@yandex.ru $
*
* Copyright 2010 by Volokitin Alexey
* All rights reserved
*
*/
#include "Common/include.h"
#include "InfoDrawer.h"

#define MIN_SIZE 240

CInfoDrawer::CInfoDrawer(IWnd* pParent)
: CDrawer(pParent), m_nPosY(0), m_nPosX(0), m_nSel(-1){}
CInfoDrawer::~CInfoDrawer(){
	Clear();
}

bool CInfoDrawer::IsValidDrawer(Drawer::typeDrawer nType){
	return nType == Drawer::info;
}

void CInfoDrawer::Paint(CDC* dc, CRect rect){
	CDC cdc;
	cdc.CreateCompatibleDC(dc);
	CBitmap bitmapTable;
	bitmapTable.CreateCompatibleBitmap(dc, rect.Size().cx, rect.Size().cy);
	HGDIOBJ bitmapOld = cdc.SelectObject(bitmapTable);
	CBrush brush;
	brush.CreateSolidBrush(GetSysColor(COLOR_3DFACE));
	cdc.FillRect(CRect(CPoint(0, 0), rect.Size()), &brush);
	brush.DeleteObject();

	int pos = m_nPosY;
	for(std::vector<CInfoItem*>::iterator it = m_aItems.begin(); it != m_aItems.end(); it++){
		(*it)->SetPosition(CPoint(m_nPosX, pos));
		pos += (*it)->GetSize().cy;
		(*it)->Paint(&cdc);
	}

	dc->BitBlt(rect.left, rect.top, rect.Size().cx, rect.Size().cy, &cdc, 0, 0, SRCCOPY);
	cdc.SelectObject(bitmapOld);
	bitmapTable.DeleteObject();
	cdc.DeleteDC();
}

void CInfoDrawer::SetSizeCx(int nSize){
	if(nSize < MIN_SIZE)
		nSize = MIN_SIZE;
	for(std::vector<CInfoItem*>::iterator it = m_aItems.begin(); it != m_aItems.end(); it++)
		(*it)->SetSizeCx(nSize);
}

int CInfoDrawer::AddItem(int nId, bool bButton){
	m_aItems.push_back(new CInfoItem(nId, this));
	m_aItems.back()->ShowButton(bButton);
	return int(m_aItems.size() - 1);
}

void CInfoDrawer::AddSubItem(int numItem, InfoItem item){
	m_aItems[numItem]->AddInfoItem(item);
}

int CInfoDrawer::InsertItem(int item, int nId, bool bButton){
	m_aItems.insert(m_aItems.begin() + item, new CInfoItem(nId, this));
	m_aItems[item]->ShowButton(bButton);
	if(item <= m_nSel)
		m_nSel++;
	return item;
}

void CInfoDrawer::SetBeginPosY(int nPos){
	m_nPosY = nPos;
}

void CInfoDrawer::SetBeginPosX(int nPos){
	m_nPosX = nPos;
}

int CInfoDrawer::GetSizeY(){
	int size = 0;
	for (std::vector<CInfoItem*>::iterator it = m_aItems.begin(); it != m_aItems.end(); it++)
		size += (*it)->GetSize().cy;
	return size;
}

int CInfoDrawer::GetMinWidth(){
	return MIN_SIZE;
}

bool CInfoDrawer::ProcessMouse(unsigned int nButton, CPoint pos, int param){
	bool bRes = false;
	if(nButton == WM_MOUSELEAVE && m_nSel != -1)
		return m_aItems[m_nSel]->ProcessMouse(WM_MOUSELEAVE, pos, param);
	if(nButton == WM_LBUTTONDOWN){
		if(m_nSel != -1 && m_nSel < (int)m_aItems.size())
			m_aItems[m_nSel]->SetSelect(false);
		m_nSel = -1;
		bRes = true;
	}
	int posit = m_nPosY;
	for(std::vector<CInfoItem*>::iterator it = m_aItems.begin(); it != m_aItems.end(); it++){
		CRect rect(CPoint(m_nPosX, posit), (*it)->GetSize());
		posit += (*it)->GetSize().cy;
		if(nButton == WM_LBUTTONDOWN && rect.PtInRect(pos)){
			m_nSel = int(it - m_aItems.begin());
			m_aItems[m_nSel]->SetSelect(true);
			(*it)->ProcessMouse(nButton, pos, param);
			return true;
		}else if(nButton == WM_MOUSEMOVE && m_nSel == int(it - m_aItems.begin()) && !rect.PtInRect(pos)){
			return (*it)->ProcessMouse(WM_MOUSELEAVE, pos, param);
		}else if(rect.PtInRect(pos) && (*it)->ProcessMouse(nButton, pos, param))
			return true;
	}
	return bRes;
}

void CInfoDrawer::SetSel(int sel){
	m_nSel = sel;
}

int CInfoDrawer::GetSel(){
	return m_nSel;
}

void CInfoDrawer::Clear(){
	for(std::vector<CInfoItem*>::iterator it = m_aItems.begin(); it != m_aItems.end(); it++)
		delete *it;
	m_aItems.clear();
	m_nSel = -1;
	m_nPosX = 0;
	m_nPosY = 0;
}

int CInfoDrawer::GetId(int sel){
	return m_aItems[sel]->GetId();
}
